﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using WineStore.Models;
using System.Data;
using PagedList;
using PagedList.Mvc;

namespace WineStore.Controllers
{
    public class LocationController : Controller
    {
        //
        // GET: /Location/

        public ActionResult ShowLocation()
        {
            using (var db = new WineDbContext())
            {
                var locations = db.Locations.ToList();
                return View(locations);
            }
        }


        // Create Location 
        [HttpGet]
        public ActionResult CreateLocation()
        {
            return View();
        }

        // Create Location 
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateLocation(Location location)
        {
            try
            {
                // Validate in server
                if (ModelState.IsValid)
                {
                    using (var db = new WineDbContext())
                    {
                        db.Entry(location).State = EntityState.Added;
                        db.SaveChanges();
                        return RedirectToAction("ShowLocation", "Location");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(location);
        }

        // GET: /Location/EditLocation/
        [HttpGet]
        public ActionResult EditLocation(int id = 0)
        {
            Location locations = null;
            using (var db = new WineDbContext())
            {
                locations = db.Locations.Find(id);
                if (locations == null)
                {
                    return HttpNotFound();
                }
            }
            return View(locations);
        }

        // POST: /Location/EditLocation/
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditLocation(Location locations)
        {
            try
            {
                using (var db = new WineDbContext())
                {
                    db.Entry(locations).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("ShowLocation", "Location");
                }

            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(locations);
        }

        // GET: /Location/DeleteLocation/
        public ActionResult DeleteLocation(int id)
        {
            Location locations = null;
            using (var db = new WineDbContext())
            {
                locations = db.Locations.Find(id);
                if (locations == null)
                {
                    return HttpNotFound();
                }
            }
            return View(locations);
        }

        // POST: /Location/DeleteLocation/
        [HttpPost, ActionName("DeleteLocation")]
        public ActionResult DeleteConfirmed(int id)
        {
            Location locations = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    locations = db.Locations.Find(id);
                    db.Entry(locations).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowLocation", "Location");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(locations);
        }

    }
}
